//
//  CNADController.m
//  BeeQuick_19
//
//  Created by 刘家强 on 16/9/23.
//  Copyright © 2016年 CN Team. All rights reserved.
//

#import "CNADController.h"

@interface CNADController ()<UIWebViewDelegate>

@property (nonatomic, weak) UIActivityIndicatorView *act;

@end

@implementation CNADController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
}

- (void)setupUI {
    
    // 创建webView
    UIWebView *webView = [[UIWebView alloc] initWithFrame:self.view.bounds];
    self.view.backgroundColor = [UIColor whiteColor];
    webView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:webView];
    [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.url]]];
    
    // 默认nav是白色的
    [self.navigationController.navigationBar setTitleTextAttributes:@{ NSForegroundColorAttributeName : [UIColor blackColor] }];
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"v2_my_avatar_bg"] forBarMetrics:UIBarMetricsDefault];
    self.title = @"AD钙奶";
    
    // 添加nav
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem barBuutonItemWithTarget:self action:@selector(close) icon:@"v2_goback" highlighticon:@"v2_goback"];
    
    webView.delegate = self;
    UIActivityIndicatorView *act = [UIActivityIndicatorView new];
    act.hidesWhenStopped = YES;
    act.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
    [webView addSubview:act];
    [act mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(webView);
    }];
    self.act = act;
}

- (void)close {
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - webView代理
- (void)webViewDidStartLoad:(UIWebView *)webView {
    [self.act startAnimating];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
    self.act.hidden = YES;
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
    self.act.hidden = YES;
}

@end
